import matplotlib as mat
import csv
import numpy as np
import matplotlib.pyplot as plt
from math import log10
# import xlrd
# plt.rcParams['font.sans-serif']=['SimHei']
# plt.rcParams['axes.unicode_minus']=False
xge=1
yge=1
name='search_Lx38erg_beta1'
seachfile=open("".join([name,'.out']),'r')
data_source=csv.reader(seachfile)
result=seachfile.readlines()
seachfile.close()
length=len(result)
timespan=[0]*len(result)
xdata=[0]*len(result)
ydata=[0]*len(result)


xindex=5
xstep=0.05
yindex=11

ystep=0.3
xlog=True
ylog=False

for i in range(len(result)):
    result[i]=result[i].split()
    result[i] = list(map(float, result[i]))
    

# resultnp=np.array(result)
# np.save("".join([name,'.npy']),resultnp)
# del result
# del resultnp

resultnp=np.load("".join([name,'.npy']))
result=resultnp.tolist()
# for i in range(len(result)):
#     timespan[i]=result[i][7]-result[i][6]
#     if(xlog):
#         xdata[i]=log10(result[i][xindex])
#     else:
#         xdata[i]=(result[i][xindex])
#     if(ylog):
#         ydata[i]=log10(result[i][yindex])
#     else:
#         ydata[i]=(result[i][yindex])

xdata=resultnp[:,xindex]
ydata=resultnp[:,yindex]
timespan=resultnp[:,7]-resultnp[:,6]
if(xlog):
        xdata=np.log10(xdata)
if(ylog):
        ydata=np.log10(ydata)
x=min(xdata)
y=min(ydata)
xrange=[]
xrangestr=[]
yrangestr=[]
yrange=[]
while (x<max(xdata)):
    xrange.append(x)
    xrangestr.append("%.2f" % x)
    x=x+xstep*xge
while(y<max(ydata)):
    yrange.append(y)
    yrangestr.append("%.2f" % y)
    y=y+ystep*yge


datamap=np.zeros((len(xrange)*xge,len(yrange)*yge))
xnpdata=np.array(xdata)
ynpdata=np.array(ydata)
xwei=((xnpdata-min(xdata))/xstep).astype(int)
ywei=((ynpdata-min(ydata))/ystep).astype(int)
for i in range(len(xdata)):
    if(timespan[i]>0.10):continue
    datamap[xwei[i]][ywei[i]]=datamap[xwei[i]][ywei[i]]+timespan[i]


# searchxls=xlrd.open_workbook("search20200325beta1,xls")

datamap=datamap/(np.max(datamap))




fig, ax = plt.subplots()
ax.imshow(datamap, interpolation='nearest',origin ='lower')
plt.xticks(range(1,np.max(ywei),xge),yrangestr)
plt.yticks(range(1,np.max(xwei),yge),xrangestr)

# plt.xlabel('campanion mass / Msun')
# plt.ylabel('orbit period / day')

plt.xlabel('m1 / Msun')
plt.ylabel('m2 / Msun')
plt.title('')
plt.show()
